{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Requirements: data from https://archive.ics.uci.edu/ml/machine-learning-databases/00396/Sales_Transactions_Dataset_Weekly.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, '../')\n",
    "\n",
    "from minisom import MiniSom\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.gridspec import GridSpec\n",
    "%matplotlib inline\n",
    "\n",
    "%load_ext autoreload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%autoreload 2\n",
    "import pandas as pd\n",
    "# data from\n",
    "# https://archive.ics.uci.edu/ml/datasets/Sales_Transactions_Dataset_Weekly\n",
    "sales_transaction = pd.read_csv('Sales_Transactions_Dataset_Weekly.csv')\n",
    "data = sales_transaction[[f'Normalized {i}' for i in range(52)]].values\n",
    "som = MiniSom(8, 8, data.shape[1], sigma=2., learning_rate=0.5, \n",
    "              neighborhood_function='gaussian', random_seed=10)\n",
    "som.pca_weights_init(data)\n",
    "print(\"Training...\")\n",
    "som.train_batch(data, 50000, verbose=True)  # random training\n",
    "print(\"\\n...ready!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "win_map = som.win_map(data)\n",
    "\n",
    "plt.figure(figsize=(16, 16))\n",
    "the_grid = GridSpec(8, 8)\n",
    "for position in win_map.keys():\n",
    "    plt.subplot(the_grid[6-position[1], position[0]])\n",
    "    plt.plot(np.min(win_map[position], axis=0), color='gray', alpha=.5)\n",
    "    plt.plot(np.mean(win_map[position], axis=0))\n",
    "    plt.plot(np.max(win_map[position], axis=0), color='gray', alpha=.5)\n",
    "plt.savefig('resulting_images/time_series.png')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
